﻿//------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace Imperium
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Forms;


    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
        "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable",
        Justification = "In a full-fledged application, the SpeechRecognitionEngine object should be properly disposed. For the sake of simplicity, we're omitting that code in this sample.")]

    public partial class MainWindow : Window
    {
        private MainWindowViewModel viewModel;
        private NotifyIcon nIcon;
        private ContextMenu cMenu;

        /// <summary>
        /// Initializes a new instance of the MainWindow class.
        /// </summary>
        public MainWindow()
        {
            // Hide app on startup, We will load into system tray.
#if !DEBUG 
            {
                Hide();
            }
#endif
            InitializeComponent();

            this.viewModel = new MainWindowViewModel();
            
            // Override Window DataContext to pass to ViewModel. This binds the properties to the XAML controls.
            this.DataContext = this.viewModel.model;

            SetupSystemTray();
        }

        // Setup app in System Tray
        private void SetupSystemTray()
        {
            try
            {
                nIcon = new NotifyIcon();
                nIcon.Icon = new System.Drawing.Icon("Images/Imperium_Icon.ico");
                nIcon.Visible = true;

                // Setup double click to restore app
                nIcon.DoubleClick +=
                    delegate(object sender, EventArgs args)
                    {
                        ShowApp();
                    };

                // setup context menu
                cMenu = new ContextMenu();
                cMenu.MenuItems.Add(0, new MenuItem("Show", new System.EventHandler(ShowClick)));
                cMenu.MenuItems.Add(1, new MenuItem("Hide", new System.EventHandler(HideClick)));
                cMenu.MenuItems.Add(2, new MenuItem("Exit", new System.EventHandler(ExitClick)));
                nIcon.ContextMenu = cMenu;

            }
            catch (Exception e)
            {
                Debug.WriteLine("{0} Could not setup System tray icon.", e);
            }
        }

        // Hide app in system tray when minimized 
        protected override void OnStateChanged(EventArgs e)
        {
            if (WindowState == WindowState.Minimized)
                this.Hide();

            base.OnStateChanged(e);
        }

        /// <summary>
        /// Execute initialization tasks.
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        public void WindowLoaded(object sender, RoutedEventArgs e)
        {
            // This section intentionally left blank.
        }

        /// <summary>
        /// Click handler for the XBMC Connection Text
        /// After reconnecting we try to start Imperium again.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StatusBarXBMCText_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.viewModel.TestXBMC();
        }

        /// <summary>
        /// Reloads the kinect and XBMC connections
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void LoadViewModel(object sender, RoutedEventArgs e)
        {
            this.viewModel = new MainWindowViewModel();
            // Override Window DataContext to pass to ViewModel. This binds the properties to the XAML controls.
            this.DataContext = this.viewModel.model;
        }

        /// <summary>
        /// Click handler for the Kinect Connection Text
        /// After reconnecting we try to start Imperium again.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StatusBarKinectText_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.viewModel.ConnectKinect();
        }

        private void ShowApp()
        {
            this.Show();
            // The "Show" method alone for the window does not bring the app to the foreground.
            // Setting the windowState is needed.
            this.WindowState = WindowState.Normal;
        }

        // Show app when click from System Tray context menu
        private void ShowClick(Object sender, System.EventArgs e)
        {
            ShowApp();
        }

        // Hides app after user click from the System Tray context menu
        private void HideClick(object sender, System.EventArgs e)
        {
            Hide();
        }

        // Exits app after user click from the System Tray context menu
        private void ExitClick(object sender, System.EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Execute uninitialization tasks.
        /// </summary>
        /// <param name="sender">object sending the event.</param>
        /// <param name="e">event arguments.</param>
        private void WindowClosing(object sender, CancelEventArgs e)
        {
            // Dispose System Tray Icon when closed. If visible isn't explicitly disposed.
            // The icon stays in the tray until the user mouses over it.
            this.nIcon.Dispose();
        }

    }
}